//当前页数
var totalnum = 0;
//总页数
var totalPage = 0;


var CaiGouDan = "0";     //采购单编号
var CaiGouFTime = "0";    //采购时间(左)
var CaiGouLTime = "0"; //采购时间(右)
var FMoneny = "0"; //金额(左)
var LMoneny = "0";  //金额(右)
var ShengHe = "0";   //审核状态


/**
 * 周永成
 * 加载完发送ajax 渲染页面
 */
$(document).ready(function () {
    queryAlllogs(0);
});

function XuanRan(result) {

    totalPage = result.totalPage;
    var str = "";

    $(".t").find('*').remove();
    if (result.data.length === 0) {
        //渲染总页数(红色)
        $(".count").html(0).css("color","red");
        //渲染页数(红色)
        $(".total").html(1).css("color","red");
        $(".t").append("<td colspan='7' style='text-align: center'>没有该类型的数据</td>");
        return;
    }
    /**
     * 开始渲染
     */
    var totalMoney = " ";
    var shengHeName = " ";
    var checkTime = " ";
    var checkStatus = " ";
    for (let i = 0; i < result.data.length; i++) {
        totalMoney = (result.data[i].totalMoney==null?" ":result.data[i].totalMoney);
        shengHeName = (result.data[i].shengHeName==null?" ":result.data[i].shengHeName);
        checkTime = (result.data[i].checkTime==null?" ": result.data[i].checkTime);
        checkStatus = (result.data[i].checkStatus == null?" ": result.data[i].checkStatus);
        str += "<tr>"+
            "<td>" + result.data[i].rownum + "</td>"+   //序号
            "<td>" + result.data[i].purchaseId + "</td>"+   //采购单编号
            "<td>" + result.data[i].purchaseTime + "</td>"+ //采购时间
            "<td> ¥" + number_format(totalMoney) + "</td>"+ //金额
            "<td>" + (result.data[i].caoZuoName) + "</td>"+   //操作人
            "<td>" + checkStatus + "</td>"+  //审核状态
            "<td>" + shengHeName + "</td>"+  //审核人
            "<td>" + checkTime + "</td>";    //审核时间

        switch (checkStatus) {
            case "未审核":
                str +=
                    "<td>" + "<a href='/purchase/purchase/purchaseUpdate.html' class='tablelink'>修改</a></td>" +     //操作
                    "<td>" + "<a href='/system/deletePurchase/"+ result.data[i].purchaseId +"' class='tablelink'>删除</a></td>" +     //操作
                    "<td>" + "<a href='/system/postPurchase/"+ result.data[i].purchaseId +"' class='tablelink'>提交审核</a></td>";    //操作
                break;
            case "审核中":
                str +=
                    "<td>" + "<a href='/system/queryPurchase/"+ result.data[i].purchaseId +"' class='tablelink'>查看详情</a></td>";    //操作
                break;
            case "审核通过":
                str +=
                    "<td>" + "<a href='/system/queryPurchase/"+ result.data[i].purchaseId +"' class='tablelink'>查看详情</a></td>"+    //操作
                    "<td>" + "<a href='/system/getJournalById/"+ result.data[i].purchaseId +"' class='tablelink'>入库管理</a></td>";    //操作
                break;
            case "审核不通过":
                str +=
                    "<td>" + "<a href='/system/deletePurchase/"+ result.data[i].purchaseId +"' class='tablelink'>删除</a></td>" +     //操作
                    "<td>" + "<a href='/system/postPurchase/"+ result.data[i].purchaseId +"' class='tablelink'>提交审核</a></td>";    //操作
                break;
        }
    }
    totalnum = result.pageNum;
    //渲染数据
    $(".t").html(str);
    str = "<li onclick='onclick_a(totalnum-1)' class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\"></span></a></li>";
    //默认第一页面
    if (totalnum === 0 || totalnum === 1){
        str += "<li onclick='onclick_a(1)' class=\"paginItem current\"><a href=\"javascript:;\">"+ 1 + "</a></li>"
    }else {
        str += "<li onclick='onclick_a(1)' class=\"paginItem\"><a href=\"javascript:;\">"+ 1 + "</a></li>"
    }
    //开始渲染分页按钮
    for (let i = 1; i <= result.totalPage; i++) {
        if (i <= 5 && i != 1) {
            if (i == totalnum) {
                str += "<li onclick='onclick_a("+ i + ")'  class=\"paginItem current\"><a href=\"javascript:;\">"+ i + "</a></li>"
            }else {
                str += "<li onclick='onclick_a("+ i + ")'  class=\"paginItem\"><a href=\"javascript:;\">"+ i + "</a></li>"
            }
        }else if (i === 6) {
            str += "<li onclick='onclick_a("+ i + ")'  class=\"paginItem more\"><a href=\"javascript:;\">...</a></li>"
        }else {
            continue;
        }
    }
    str +="<li onclick='onclick_a("+ (totalnum + 1) + ")'  class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\"></span></a></li>";
    //渲染总页数(红色)
    $(".count").html(result.totalRows).css("color","red");
    //渲染页数(红色)
    $(".total").html(result.pageNum).css("color","red");
    //渲染分页按钮
    $(".paginList").html(str);
}

/**
 * 渲染页面 (分页)
 * 周永成
 */
function queryAlllogs(num) {
    $.ajax({
        url:'/purchase/GetList/'+CaiGouDan+"/"+CaiGouFTime+"/"+CaiGouLTime+"/"+FMoneny+"/"+LMoneny+"/"+ShengHe+"/"+num,
        type:'post',
        dataType:'json',
        success:function (result) {
            alert("YES");
            XuanRan(result)
        },
        error:function () {
            alert("no")
        }
    });
}




/**
 *  周永成
 *  点击按钮事件
 */
function onclick_a(index) {

    if (index <= 0 || index > totalPage){
        alert("已经是最后一页了")
    } else {
        var list = $(".paginList").find('li');

        for (var i = 0; i < list.length; i++) {
            $(list[i]).attr("class","paginItem");
        }

        $(list[index]).attr("class","paginItem current");
        queryAlllogs(index);
    }

}

/**
 * 周永成
 * 模糊查询
 */
function serach() {

    totalnum = 0;
    CaiGouDan = $("input[name=CaiGouDan]").val();   //采购单编号
    CaiGouDan = CaiGouDan ==""?"0":CaiGouDan;
    CaiGouFTime = $("input[name=CaiGouFTime]").val().split(' ')[0]; //采购时间(左)
    CaiGouFTime = CaiGouFTime ==""?"0":CaiGouFTime;
    CaiGouLTime = $("input[name=CaiGouLTime]").val().split(' ')[0]; //采购时间(右)
    CaiGouLTime = CaiGouLTime ==""?"0":CaiGouLTime;
    FMoneny = $("input[name=FMoneny]").val();   //金额(左)
    FMoneny = FMoneny ==""?"0":FMoneny;
    LMoneny = ($("input[name=LMoneny]").val()); //金额(右)
    LMoneny = LMoneny==""?"0":LMoneny;
    ShengHe = $('#ShengHe option:selected').val(); //审核状态
    ShengHe = ShengHe=="请选择"?"0":ShengHe;
    queryAlllogs(totalnum);
}

/**
 * 周永成
 * 数字逗号隔开
 */

function number_format(str){
    var b=parseInt(str).toString();
    var len = b.length;
    if(len<=3){return b;}
    var r = len%3;
    return r>0?b.slice(0,r)+","+b.slice(r,len).match(/\d{3}/g).join(","):b.slice(r,len).match(/\d{3}/g).join(",");
}

